package com.zhida.smartcontrol.pagectrl.projectdetail.zonemanager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.jay.tools.recycler.MultipleViewHolder;
import com.jay.tools.recycler.SingleRecyclerAdapter;
import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.ui.Cbox;
import com.jay.tools.utils.ResUtil;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("SetTextI18n")
public class JkqAdapter extends SingleRecyclerAdapter<LampBean> {
    private int mColorGreen, mColorRed;
    private int mBlue, mGray;

    public JkqAdapter(@Nullable List<LampBean> data) {
        super(R.layout.item_jkq, data);
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        Context context = recyclerView.getContext();
        mColorGreen = ResUtil.getColor(context, R.color.c_22D49C);
        mColorRed = ResUtil.getColor(context, R.color.c_FF4D62);
        mBlue = ResUtil.getColor(context, R.color.c_3862FF);
        mGray = ResUtil.getColor(context, R.color.c_999999);
    }

    @Override
    protected void convert(MultipleViewHolder holder, LampBean info) {
        LinearLayout ddItem = holder.getView(R.id.ll_dd);
        Cbox cbox = holder.getView(R.id.cbox);
        TextView tvTime = holder.getView(R.id.tv_time);
        TextView tvAddress = holder.getView(R.id.tv_address);
        TextView tvMode = holder.getView(R.id.tv_mode);
        TextView tvXiu = holder.getView(R.id.tv_xiu);
        TextView tvYingjianbanben = holder.getView(R.id.tv_yingjianbanben);
        TextView tvRuanjianbanben = holder.getView(R.id.tv_ruanjianbanben);
        TextView tvJiaoshi = holder.getView(R.id.tv_jiao_shi);
        TextView tvNum = holder.getView(R.id.tv_lamp_num);
        TextView tvData = holder.getView(R.id.tv_data);
        ImageView iv0 = holder.getView(R.id.iv_0);
        ImageView iv1 = holder.getView(R.id.iv_1);
        ImageView iv2 = holder.getView(R.id.iv_2);
        ImageView iv3 = holder.getView(R.id.iv_3);
        ImageView iv4 = holder.getView(R.id.iv_4);
        ImageView iv5 = holder.getView(R.id.iv_5);
        ImageView iv6 = holder.getView(R.id.iv_6);
        ImageView iv7 = holder.getView(R.id.iv_7);
        ButtonStyle btnEdit = holder.getView(R.id.btn_edit);
        ButtonStyle btnDel = holder.getView(R.id.btn_del);
        TextView tvOnline = holder.getView(R.id.tv_online);
        tvYingjianbanben.setText("H:" + (info.getHowManyTxt().equals("") ? "未设定" : info.getHowManyTxt()));
        tvRuanjianbanben.setText("S:" + (info.getSoftwareVersion().equals("") ? "未设定" : info.getSoftwareVersion()));
        if (info.getOnlineStatus().equals("1")) {
            tvOnline.setText("在线");
            tvOnline.setTextColor(mBlue);
        } else {
            tvOnline.setText("离线");
            tvOnline.setTextColor(mGray);
        }

        cbox.setText(info.getImei());
        cbox.setChecked(info.isCheck());
        cbox.setOnCboxClickListener(new Cbox.OnCboxClickListener() {
            @Override
            public void onCboxClick(View v) {
                Cbox box = (Cbox) v;
                info.setCheck(box.isChecked());
            }
        });
        tvAddress.setText(info.getAddress());
        switch (info.getControlType()) {
            case Const.HAND:
                tvMode.setVisibility(View.VISIBLE);
                tvMode.setText("手");
                tvXiu.setVisibility(View.GONE);
                break;
            case Const.AUTO:
                tvMode.setVisibility(View.VISIBLE);
                tvMode.setText("自");
                tvXiu.setVisibility(View.GONE);
                break;
            case Const.XIUZI:
                tvMode.setVisibility(View.VISIBLE);
                tvMode.setText("自");
                tvXiu.setVisibility(View.VISIBLE);
                tvXiu.setText("休");
                break;
            case Const.XIUSHOU:
                tvMode.setVisibility(View.VISIBLE);
                tvMode.setText("手");
                tvXiu.setVisibility(View.VISIBLE);
                tvXiu.setText("休");
                break;
            case Const.JIANXIU:
                tvMode.setVisibility(View.VISIBLE);
                tvMode.setText("检");
                tvMode.setTextColor(Color.parseColor("#c_FF4D62"));
                tvXiu.setVisibility(View.GONE);
                break;
            default:
                tvMode.setVisibility(View.GONE);
                tvXiu.setVisibility(View.GONE);
                break;
        }

        tvJiaoshi.setText("校时：" + (info.getClockUpdateTime().equals("") ? "未曾做校时" : info.getClockUpdateTime()));
        if (info.getUpdateTimeFlag().equals("0")) {
            tvJiaoshi.setTextColor(mColorGreen);
        } else {
            tvJiaoshi.setTextColor(mColorRed);
        }

        tvTime.setText("更新：" + info.getUpdateDate());
        if (info.getUpdateStatus().equals("0")) {
            tvTime.setTextColor(mColorGreen);
        } else {
            tvTime.setTextColor(mColorRed);
        }

        List<ImageView> ivList = new ArrayList<>();
        ivList.add(iv0);
        ivList.add(iv1);
        ivList.add(iv2);
        ivList.add(iv3);
        ivList.add(iv4);
        ivList.add(iv5);
        ivList.add(iv6);
        ivList.add(iv7);
        setChannel(ivList, info.getSwitchType(), info.getReturnValueTxt());

        tvData.setOnClickListener(new ClickListener(info, 0));

        tvNum.setText(info.getChildrenCount() + "");
        ddItem.setOnClickListener(new ClickListener(info, 1));

        btnEdit.setOnClickListener(new ClickListener(info, 2));

        btnDel.setOnClickListener(new ClickListener(info, 3));

    }

    private class ClickListener implements View.OnClickListener {
        private LampBean mInfo;
        private int mClickId;

        public ClickListener(LampBean mInfo, int mClickId) {
            this.mInfo = mInfo;
            this.mClickId = mClickId;
        }

        @Override
        public void onClick(View v) {
            if (null != mListener) {
                mListener.onClick(mInfo, mClickId);
            }
        }
    }

    // 设置通道状态  // 0关闭， 1开启
    private void setChannel(List<ImageView> ivList, String switchType, String returnValueTxt) {
        int num = getChannelNum(switchType);
        String valueTxt = new StringBuffer(returnValueTxt).reverse().toString();
        String[] split = valueTxt.split("");
        int size = ivList.size();
        for (int i = 0; i < size; i++) {
            ImageView iv = ivList.get(i);
            if (i < num) {
                iv.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(split[0])) {
                    iv.setImageResource(getRes(split[i + 1]));
                } else {
                    iv.setImageResource(getRes(split[i]));
                }
            } else {
                iv.setVisibility(View.GONE);
            }
        }
    }

    // 获取通道数量
    private int getChannelNum(String switchType) {
        int type = Integer.parseInt(switchType);
        int index;
        switch (type) {
            case Const.NB_SINGLE:
            case Const.ZIG_BEE_SINGLE:
                index = 1;
                break;
            case Const.NB_DOUBLE:
                index = 2;
                break;
            case Const.SWITCH:
                index = 8;
                break;
            default:
                index = 4;
                break;
        }
        return index;
    }

    private int getRes(String state) {
        if (state.equals("1")) { //1,开启
            return R.drawable.ic_td_open;
        } else {// if (state.equals("0")) { //0,关闭
            return R.drawable.ic_td_close;
        }
    }

}
